<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use app\library\Flag;
use think\Model;
use think\Db;

class Note extends Model
{
    protected $table = 'note';

    protected $pk = 'id';

    /**
     * 申请圈子
     *
     */
    public function addNote($arrInput)
    {
        if (!empty($arrInput['extend_info'])) {
            if (is_array($arrInput['extend_info'])) {
                $arrInput['extend_info'] = json_encode($arrInput['extend_info']);
            }
        } else {
            $arrInput['extend_info'] = '[]';
        }

        unset($arrInput[$this->pk]);

        $this->data($arrInput);
        $this->save();
        return $this->data[$this->pk];
    }

    public function delNote($id)
    {
        return Db::table($this->table)->where(['id' => ['=', $id]])->update(['is_delete' => 1]);
    }

    /**
     * 获取帖子列表
     *
     */
    public function getNote($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    /**
     * 根据帖子id获取帖子详情
     *
     */
    public function getNoteByid($id, $fields = [])
    {
        $this->allowField(true);

        $arrCond = [
            'id' => ['=', $id]
        ];

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        $ret = Db::table($this->table)->where($arrCond)->field($arrField)->select();

        if (false === $ret) {
            return false;
        }

        if (isset($ret[0]) && !empty($ret[0])) {
            return $ret[0];
        }

        return [];
    }

    /**
     * 根据帖子id批量获取帖子详情
     *
     */
    public function getNoteByids($ids, $fields = [])
    {
        $this->allowField(true);

        $arrCond = [
            'id' => ['in', $ids]
        ];

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        $ret = Db::table($this->table)->where($arrCond)->field($arrField)->select();

        if (empty($ret)) {
            return [];
        }

        return $ret;
    }

    /**
     * 获取帖子列表
     *
     */
    public function getNoteCount($conds)
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        if (empty($arrCond)) {
            return 0;
        }

        return Db::table($this->table)->where($arrCond)->count();
    }

    /**
     * 获取帖子排名
     *
     */
    public function getNoteRankByCirid($cir_id)
    {
        if (!$cir_id) {
            return false;
        }

        $sql = 'select creator,count(*) as note_cnt from note where cir_id=' . $cir_id . ' group by creator order by note_cnt desc';

        return Db::table($this->table)->query($sql);
    }

    /**
     * 获取发帖数
     *
     */
    public function getNoteCountByUserids($userids, $conds = [])
    {
        if (empty($userids)) {
            return [];
        }

        $this->allowField(true);

        $strConds = '';
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $strConds .= $key . $val . ' ';
            }
        }

        if ($strConds) {
            $strConds = ' and ' . $strConds;
        }

        $sql = sprintf('select creator,count(*) as note_cnt from %s where creator in (%s) and approve_status=%d and is_delete=%d  %s  group by creator', $this->table, implode(',', $userids), Flag::CONST_NOTE_APPROVE_PASS, Flag::CONST_NOTE_DELETE_NOT, $strConds);

        return Db::table($this->table)->query($sql);
    }

    /**
     * 审核帖子
     *
     */
    public function approveNote($arrInput)
    {
        $conds = [
            'id' => ['=', $arrInput['note_id']]
        ];

        $data = [
            'approve_status' => $arrInput['status'],
            'approve_time' => time(),
            'update_time' => time()
        ];

        if (isset($arrInput['reason']) && !empty($arrInput['reason'])) {
            $data['reason'] = $arrInput['reason'];
        }

        return Db::table($this->table)->where($conds)->update($data);
    }

    public function updateNote($arrInput)
    {
        $conds = [
            'id' => ['=', $arrInput['note_id']]
        ];

        $arrInput['update_time'] = time();

        return Db::table($this->table)->where($conds)->update($arrInput);
    }

    /**
     * 更新flag
     *
     */
    public function updateFlag($arrInput)
    {
        $conds = [
            'id' => ['=', $arrInput['note_id']]
        ];

        $data = [
            'flag' => $arrInput['flag'],
            'update_time' => time()
        ];

        if (($arrInput['flag'] & Flag::FLAG_NOTE_ORIGIN) == Flag::FLAG_NOTE_ORIGIN) {
            $data['original'] = 1;
        }

        return Db::table($this->table)->where($conds)->update($data);
    }

    /**
     * @return \think\paginator\Collection
     */
    public function noteListPage()
    {
        $conds = [
            'is_delete' => 0,
        ];
        return $this->where($conds)->paginate(10);
    }


    public function searchNote($wd)
    {
        $sql = "select id from note where is_delete=0 and ((content like '%{$wd}%') or (title like '%{$wd}%'))";

        $ret = Db::table($this->table)->query($sql);
        if (empty($ret)) {
            return [];
        }
        return $ret;
    }
}
